$(function () {
    const add_url = "http://localhost:9090/book/add";
    const update_url = "http://localhost:9090/book/update";
    let bookmamager = new BookManager();
    let tbody = $("#tbody");
    bookmamager.load(tbody);

    $('.addBtn').on("click", () => {
        let bName = $('#bName').val();
        let price = $('#price').val();
        let author = $('#author').val();
        if (bName === "" || price === "" || author === "") {
            return false;
        }
        let data = {
            bName: bName,
            price: price,
            author: author
        };
        myAjax.request(add_url, data, "POST", (e) => {
            if (e.msg === "success") {
                bookmamager.renderOne(e.book, tbody); //不重新渲染表格，只往后添加一行
                bookmamager.clearInput();
            }
        })
    });

    $('.updateBtn').on("click", () => {
        let bName = $('#ubName').val();
        let price = $('#uprice').val();
        let author = $('#uauthor').val();
        let id = $('#id').val();
        if (bName === "" || price === "" || author === "") {
            return false;
        }
        let data = {
            bName: bName,
            price: price,
            author: author,
            id: id
        };
        myAjax.request(update_url, data, "PUT", (e) => {
            if (e.msg === "success")
                bookmamager.renderTr(e.book);
        })
    })

});

function BookManager() {

    const all_url = "http://localhost:9090/book/all";
    const delete_url = "http://localhost:9090/book/delete";

    this.load = function (tbody) {
        $.get(all_url, (e) => {
            this.renderTable(e, tbody);
        });
    };

    this.renderTable = function (datas, tbody) {
        $.each(datas, (index, val) => {
            this.renderOne(val, tbody);
        })
    };

    this.renderOne = function (val, tbody) {
        let tr = $('#tr-temp').clone(true, true).removeAttr("style").attr("id", val.id).appendTo(tbody);
        tr.find("td:eq(0)").text(val.bName);
        tr.find("td:eq(1)").text(val.price);
        tr.find("td:eq(2)").text(val.author);
        tr.find("td:eq(3)").text(timeFormat.formatlistdate(val.pressDate));
        $('.btn-danger').off('click').on('click', function () {
            myAjax.request(delete_url, {id: val.id}, "DELETE", (e) => {
                if (e.msg == "success") {
                    tr.remove(); //不重新渲染，只删除当前行
                }
            });
        });
        tr.dblclick(() => {
            $("#updateModal").modal();
            $('#ubName').val(val.bName);
            $('#uprice').val(val.price);
            $('#uauthor').val(val.author);
            $('#id').val(val.id);
        });
    };

    this.renderTr = function (data) { //不重新渲染，只更新某一行数据
        let tr = $('#' + data.id);
        tr.children(".name").text(data.bName);
        tr.children(".price").text(data.price);
        tr.children(".author").text(data.author);
    };

    this.clearInput = function () {
        $('#addForm').find(":input").val('');
    }

}
